const Base = require('./base.js');
const fs = require('fs');

module.exports = class extends Base {
  async avatarAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/avatar/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename
    });
  }

  async bannerAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/banner/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename
    });
  }

  async newsThumbAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/news/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename
    });
  }

  async checkVipAction() {
    await this.model('user').where('DATEDIFF(curdate(),end_time)>0').update({
      is_vip: 0
    });

    await this.model('figure').where('DATEDIFF(curdate(),dated_time)>0').update({
      is_vip: 0
    });

    return this.success();
  }
};
